# -*- coding: utf-8 -*-
# filename: media.py
from basic import Basic
import urllib2
import poster.encode
import json
from poster.streaminghttp import register_openers

class Media(object):
    def __init__(self):
        register_openers()
    #上传图片
    def uplaod(self, accessToken, filePath, mediaType):
        openFile = open(filePath, "rb")
        param = {'media': openFile}
        postData, postHeaders = poster.encode.multipart_encode(param)

        postUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s" % (accessToken, mediaType)
        request = urllib2.Request(postUrl, postData, postHeaders)
        urlResp = urllib2.urlopen(request)
        print urlResp.read()

    def get(self, accessToken, mediaId):
        postUrl = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s" % (accessToken, mediaId)
        urlResp = urllib2.urlopen(postUrl)
        print urlResp
        headers = urlResp.info().__dict__['headers']
        print headers
        #if ('Content-Type: application/json\r\n' in headers) or ('Content-Type: text/plain\r\n' in headers):
         #   jsonDict = json.loads(urlResp.read())
          #  print jsonDict
        #else:
        buffer = urlResp.read()   #素材的二进制
        mediaFile = file("test_media.jpg", "wb")
        mediaFile.write(buffer)
        print "get successful"

if __name__ == '__main__':
    myMedia = Media()
    accessToken = Basic().get_access_token()
    filePath = "media/pic/2017002.jpg"   #请安实际填写
    mediaType = "image"
    mediaId = str(myMedia.uplaod(accessToken, filePath, mediaType))
    print mediaId
    if mediaId is None:
        print "LJF->mediaId is None"
    else:
        print "LJF->mediaId is not None"
    print type(mediaId)
    #xxx = str(mediaId)+""
    #print xxx
    #print type(xxx)
    #dd = json.load(mediaId)
    dd = json.dumps(mediaId)
    myMedia.get(accessToken, ['media_id'])
